﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.OleDb;

using SEiBuyAgency.Factory;

namespace SEiBuyAgency.User
{
    class CUser
    {

        string conn = System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"];

        public IUserActionObserver myObserver{get;set;}
        public string Id { get; set; }
        public int dis { get; set; }
        public string password { get; set; }
        public string email { get; set; }


        public void Regist(string id, string pwd, string email)
        {
            OleDbConnection aConnection = new OleDbConnection(conn);

            string sqlt = "insert into [User](userid,userpwd,useremail) values('"+id+"','"+pwd+"','"+email+"');";
            int result=-1;
            try
            {
                 aConnection.Open();
                 OleDbCommand cmd = new OleDbCommand(sqlt, aConnection);
                 result = cmd.ExecuteNonQuery();
                aConnection.Close();
            }
            catch (Exception)
            {
                result = 0;
            }
            if(result==1)
                Completely(TAction.ERegist, true);
            else
                Completely(TAction.ERegist, false);
        }

        public void Login(string id)
        {
            //do something
            Completely(TAction.ELogin, true);
        }

        public void Logout(string id)
        {
            //
            Completely(TAction.Elogout, true);
        }

        public void ChancgePwd(string id, string newPwd, string newEmail)
        {
           OleDbConnection aConnection = new OleDbConnection(conn);

           string sqlt = @"UPDATE [User] SET userpwd = '" + newPwd + @"',userEmail = '" + newEmail + @"' WHERE userid='" + id + @"';";

            int result = -1;
            try
            {
                aConnection.Open();
                OleDbCommand cmd = new OleDbCommand(sqlt, aConnection);
                result = cmd.ExecuteNonQuery();
                aConnection.Close();
            }
            catch (Exception)
            {
                result = 0;
                aConnection.Close();
            }


            if (result > 0)
                Completely(TAction.EChangePwd, true);
            else
                Completely(TAction.EChangePwd, false);
        }

        virtual public void Buy(string  productID, int cout)
        {
            //
            Completely(TAction.EBuy, true);
        }

        public void FeedBack(string productID, string text)
        {
            //
            Completely(TAction.EFeedBack, true);
        }
        
        protected void Completely( TAction action, bool suc)
        {
            myObserver.Completely(action, suc);
        }

        protected bool  rmFromGoods(string id, int amount)
        {
            OleDbConnection aConnection = new OleDbConnection(conn);

            string sqlt = "update goods set goodsamount=goodsamount-"+amount+" where goodsID='"+id+"'";
            int result = -1;
            try
            {
                aConnection.Open();
                OleDbCommand cmd = new OleDbCommand(sqlt, aConnection);
                result = cmd.ExecuteNonQuery();
                aConnection.Close();
            }
            catch (Exception)
            {
                result = 0;
                aConnection.Close();
            }

            if (result == 1)
                return true;
            else
                return false;
        }

        protected bool insertOrder(string userId,string goodsId, int goodsnumber,string dis)
        {
            OleDbConnection aConnection = new OleDbConnection(conn);

            string sqlt = @"insert into userorder (orderuserid,ordergoodsid,ordernumber,orderdiscount) values('"+this.Id+@"','"+goodsId+@"',"+goodsnumber.ToString()+@",'"+dis+"');";
            
            //values('" + userId + @"','" + goodsId + @"'," + goodsnumber.ToString() + @");";
            int result = -1;
            try
            {
                aConnection.Open();
                OleDbCommand cmd = new OleDbCommand(sqlt, aConnection);
                result = cmd.ExecuteNonQuery();
                aConnection.Close();
            }
            catch (Exception)
            {
                result = 0;
            }

            if (result == 1)
                return true;
            else
                return false;
        }

    }
}
